package com.itheima.health.h5.controller;

import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Order;
import com.itheima.health.service.OrderService;
import com.itheima.health.utils.redis.RedisUtil;
import com.itheima.health.utils.resources.RedisConstant;
import com.itheima.health.utils.resources.RedisMessageConstant;
import com.itheima.health.utils.sms.SmsUtils;
import com.itheima.health.utils.sms.ValidateCodeUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
public class OrderController {

    @Reference(mock = "com.itheima.health.service.OrderServiceMock",timeout = 1500)
    private OrderService orderService;

    @Autowired
    private RabbitTemplate rabbitTemplate;


    @GetMapping("order/sendMessage/{telephone}")
    public Result sendMessage(@PathVariable("telephone")String  telephone){
        //  存放 redis    阿里云发送短信
        String code = ValidateCodeUtils.generateValidateCode(4);
        RedisUtil.set(RedisMessageConstant.SENDTYPE_ORDER+telephone,code,90, TimeUnit.SECONDS);
       // mq  发送发短信内容 消息形式 投递 mq  队列上
        Map map = new HashMap();
        map.put("telephone",telephone);
        map.put("code",code);
        rabbitTemplate.convertAndSend("heimait",map);
        System.out.println(code);
        return  new Result(code);
    }

    @GetMapping("order/findOrderInfoById/{id}")
    public Result findOrderInfoById(@PathVariable("id")int  id){
        return  new Result(orderService.findOrderInfoById(id));
    }

    @PostMapping("order/add")
    public Result add(@RequestBody Map map){
        Result result = new Result(orderService.add(map));
        return result ;
    }

    @GetMapping("order/timeout")
    public Result timeout(){
        Result result = new Result();
        result.setMessage("页面太火爆，请择日再约");
        return result;
    }


}
